package cn.com.bluemoon.daps.standard.controller;


import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import cn.com.bluemoon.daps.common.mp.IBmService;
import cn.com.bluemoon.daps.common.webhook.Authors;
import cn.com.bluemoon.daps.standard.service.DapStandardAuditService;
import cn.com.bluemoon.daps.standard.vo.StandardDataModifyVo;
import cn.com.bluemoon.daps.standard.vo.StandardListSearchVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 数据标准的核心表，记录各标准的版本和审核状态 前端控制器
 * </p>
 * 审核列表
 * 变更列表
 * 基础标准crud
 * 指标标准crud
 * 批量导入
 *
 * @author admin
 * @since 2020-08-07
 */
@Api(tags = "标准审批列表")
@RestController
@Slf4j
@Authors(value = @Authors.Author(name = "Jarod.Kong", desc = "标准审批列表", devTime = "2020年11月18日11:23"))
@RequestMapping("/dapStandardAudit")
public class DapStandardAuditController {

    @Resource
    private DapStandardAuditService dapStandardAuditService;

    @Authors(value = @Authors.Author(name = "Jarod.Kong", desc = "根据查询条件获取审核列表数据", devTime = "2020年11月18日11:23"))
    @BmPermission(value = {"standard:audit:basic:query", "standard:audit:index:query"}, name = "标准审批查询")
    @PostMapping("/audit/list")
    @ApiOperation(value = "根据查询条件获取审核列表数据")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = StandardDataModifyVo.class)})
    public ResultBean<?> auditList(@RequestBody @Valid StandardListSearchVo<?> standardListSearchVo) {
        IPage<StandardDataModifyVo> standardDataModifyVoPage = dapStandardAuditService.auditList(standardListSearchVo);
        return IBmService.toAntDvPageTool(standardDataModifyVoPage);
    }

    @BmPermission(value = {"standard:audit:basic:pass", "standard:audit:index:pass"}, name = "标准审批-通过")
    @PutMapping("/pass")
    @ApiOperation(value = "根据流程id进行通过待审批")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ResultBean.class)})
//    @UserPermission.SingleRow(enabled = true, key = "id", keyFormClz = KeyAnnotationClz.RequestBody, modelClz = DapStandardAuditVersionMain.class)
    public ResultBean<?> passAudit(@RequestBody List<String> ids,
                                   @Valid @Length(max = 200, message = "变更说明长度不大于{max}")
                                   @RequestParam("modifyDesc") String modifyDesc) {
        dapStandardAuditService.passAudit(ids.toArray(new String[0]));
        // 通过后进行版本记录（根据id找到数据，分别记录相关内容）
        dapStandardAuditService.saveStandardVersion(ids, modifyDesc, UserInfoHolder.getUserInfoOrEmpty());
        return ResultBean.ok();
    }

    @BmPermission(value = {"standard:audit:basic:reject", "standard:audit:index:reject"}, name = "标准审批-不通过")
    @PutMapping("/unpass")
    @ApiOperation(value = "根据流程id进行不通过待审批")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ResultBean.class)})
    public ResultBean<?> unPassAudit(@RequestBody List<String> ids) {
//        ids.forEach(id -> dapStandardAuditService.unpassAudit(id));
        dapStandardAuditService.unpassAudit(ids.toArray(new String[0]));
        return ResultBean.ok();
    }

    @BmPermission(value = {"standard:audit:basic:query", "standard:audit:index:query"}, name = "标准审批-查询")
    @GetMapping("/versions/{standardUniqueId}")
    @ApiOperation(value = "根据标准唯一id获取标准所有版本信息，树形")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ResultBean.class)})
    public ResultBean<?> versions(@PathVariable String standardUniqueId) {
        List<StandardDataModifyVo> dataModifyVos = dapStandardAuditService.viewVersions(standardUniqueId, true);
        return ResultBean.ok(dataModifyVos);
    }
}
